package structuralModel.agentModel.dynamicProxy.myAnnotation;

import okhttp3.Request;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 功能描述:用户服务方面
 * 2024/08/26
 *
 * @author ZhangYi
 */
@Component
@Aspect
public class UserServiceAspect {
	/**
	 * 密钥id
	 */
	private final static String KEY_ID ="12312kljaldfas";
	/**
	 * 密钥代码
	 */
	private final static String KEY_CODE = "4232-manager";

	@Pointcut("@annotation(structuralModel.agentModel.dynamicProxy.myAnnotation.InjectUserServiceHeader)")
	public void userServicePointCut(){

	}

	@Around(value = "userServicePointCut()")
	public Object around(ProceedingJoinPoint joinPoint){
		try {
			// joinPoint.proceed(joinPoint.getArgs()); 先执行 注解中的方法,
			Object proceed = joinPoint.proceed(joinPoint.getArgs());
			// 执行后续 横切的代码
			if (proceed instanceof Request.Builder){
				Request.Builder requestBuilder = (Request.Builder) proceed;
				requestBuilder.addHeader("keyId",KEY_ID);
				requestBuilder.addHeader("keyCode",KEY_CODE);
				return requestBuilder;
			}
			return proceed;
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
	}
}
